package com.woniuxy.third.handler;

import com.woniuxy.third.enums.CourseLiveEnum;
import com.woniuxy.third.enums.CourseStatusEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CourseLiveEnumHandler extends BaseTypeHandler<CourseLiveEnum> {
    public CourseLiveEnumHandler(Class<CourseStatusEnum> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
    }

    private CourseLiveEnum locateEnum(int value) {
        for(CourseLiveEnum courseLiveEnum:CourseLiveEnum.values()){
            if(courseLiveEnum.getCode()==value){
                return courseLiveEnum;
            }
        }
        throw new IllegalArgumentException("未知的枚举类型：" + value);
    }

    public void setNonNullParameter(PreparedStatement preparedStatement, int i, CourseLiveEnum courseLiveEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, courseLiveEnum.getCode());
    }

    public CourseLiveEnum getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        int i = resultSet.getInt(columnName);
        return resultSet.wasNull() ? null : this.locateEnum(i);
    }

    public CourseLiveEnum getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
        int i = resultSet.getInt(columnIndex);
        return resultSet.wasNull() ? null : this.locateEnum(i);
    }

    public CourseLiveEnum getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        int i = callableStatement.getInt(columnIndex);
        return callableStatement.wasNull() ? null : this.locateEnum(i);
    }

    public CourseLiveEnumHandler() {
    }
}
